/**
 * @author ?????
 * @createTime 2019-1-25 ????3:09:44
 */

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ?????
 * @createTime 2019-1-25 ????3:09:44
 */
public class TOOL {
    private static final String APPLICATION_JSON = "application/json";
    private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
    public static String urlH = "http://127.0.0.1:9980/service/";

    public static void main(String[] args) throws Exception {
        // StringBuffer fileNameUrl = new StringBuffer();
        String targetString = "";
        String urlend = "";
        String key = "";
        // ?
        String value = "";
        String fileNameUrl = "C:\\Users\\79\\Desktop\\test.xlsx";
        System.out.println("开始");
        List<String> targList = new ArrayList<String>();
        List<Map<String, String>> jsonList = new ArrayList();
        // ???excel???????
        File file = new File(fileNameUrl);
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(file));
        if (workbook == null) {
            System.out.println("文件不存在");
            return;
        }

        XSSFSheet s = workbook.getSheetAt(0);// ?????????????????
        //row  行
        // cell  列
        int lastRowNum = s.getLastRowNum();
        //拼接url
        urlend = s.getRow(0).getCell(0).toString();
        urlH += urlend;

        //第一行 用来取字段名
        Row rowOne = s.getRow(0);


        //循环行 从第二行开始
        for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
            Map<String, String> map = new HashMap<String, String>();
            //得到行  用行获取列
            Row row = s.getRow(rowNum);
            targetString = row.getCell(1).toString();
            // 从第三列开始取值
            for (int cellNum = 2; cellNum < row.getLastCellNum(); cellNum++) {
                //第一行的字段名
                key = rowOne.getCell(cellNum).toString();
                if (row.getCell(cellNum) != null) {
                    value = row.getCell(cellNum).toString();
                } else {
                    value = "";
                }
                map.put(key, value);
            }
            jsonList.add(map);
            targList.add(targetString);
        }



        List insertExcelList = new ArrayList();
        for (int i = 0; i < jsonList.size(); i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            if (jsonList.get(i) != null) {
                String startTime = getNowTime(new Date());
                // 执行http方法
                String result = doPost(urlH, JSON.toJSONString(jsonList.get(i)));
//                String result = "232";
                String endTime = getNowTime(new Date());
                map.put("startime", startTime);
                map.put("endtime", endTime);
                map.put("json", JSON.toJSONString(jsonList.get(i)));
                map.put("result", result);
                map.put("targetString", targList.get(i));
                insertExcelList.add(map);
            }
        }

        insertExcel(insertExcelList, workbook);
        System.out.println("完成");

    }


    public static void insertExcel(List<Map<String, Object>> list,
                                   XSSFWorkbook workbook) throws IOException {
        XSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 11);
        font.setFontName("??????");
//		font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        XSSFCellStyle style = workbook.createCellStyle();
        style.setFont(font);
//		style.setAlignment(XSSFCellStyle.SOLID_FOREGROUND);
        XSSFSheet s = workbook.getSheetAt(1);

        XSSFRow row = s.createRow(0);
        row.createCell(1).setCellValue("startime");
        row.createCell(2).setCellValue("endtime");
        row.createCell(3).setCellValue("json");
        row.createCell(4).setCellValue("result");
        row.createCell(5).setCellValue("targetString");

        // ?????
        for (int j = 0; j < list.size(); j++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map = list.get(j);

            XSSFRow row2 = s.createRow(j);
            row2.createCell(1).setCellValue((String) map.get("startime"));
            row2.createCell(2).setCellValue((String) map.get("endtime"));
            row2.createCell(3).setCellValue((String) map.get("json"));
            row2.createCell(4).setCellValue((String) map.get("result"));
            row2.createCell(5).setCellValue((String) map.get("targetString"));
        }
        FileOutputStream fo = new FileOutputStream(
                "C:\\Users\\79\\Desktop\\test2.xlsx"); // ????????
        workbook.write(fo);
    }

    public static String doPost(String url, String json) throws Exception {
        // ??JSON????UTF-8????,?????????
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        StringEntity se = new StringEntity(json);
        se.setContentType("text/json");
        se.setContentEncoding("application/json");
        httpPost.setEntity(se);
        HttpResponse response = httpClient.execute(httpPost);

        if (response.getStatusLine().getStatusCode() != 200) {
            System.out.println("???????????");
            return "";
        }

        String strResult = EntityUtils.toString(response.getEntity());
        return strResult;
    }


    public static String getNowTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

}
